# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Shortcut plugin is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Shortcut plugin is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Este es un plugin de prueba
# propiedad de iAgree
VERSION = '0.1'

import Plugin
import dbus, subprocess

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    description = _('Integrates with Compiz-Fusion through Dbus.')
    authors = {'iAgree':''}
    website = 'http://emesene.org'
    displayName = _('Compiz Integration')
    name = 'Compiz'

    def __init__(self, controller, msn):
        '''Contructor'''

        Plugin.Plugin.__init__(self, controller, msn )

        self.description = _('Integrates with Compiz-Fusion through Dbus.')
        self.authors = {'iAgree':''}
        self.website = ''
        self.displayName = _('Compiz Integration')
        self.name = 'Compiz'

        self.controller = controller
    
    def start(self):

        # obtain information about the root window
        self.rootWindow = self.getWinID()

        # initiate dbus class
        self.DbusBase = dbus.SessionBus()
        self.proxy = self.DbusBase.get_object('org.freedesktop.compiz',
            '/org/freedesktop/compiz/water/allscreens/point')
        self.obj = dbus.Interface(self.proxy, 'org.freedesktop.compiz')

        # hack-hook
        self._set_blinking = self.controller.trayIcon.tray.set_blinking
        self.controller.trayIcon.tray.set_blinking = self.set_blinking

        self.enabled = True

    def stop(self):

        # revert hacks
        self.controller.trayIcon.tray.set_blinking = self._set_blinking
        del self._set_blinking

        #TODO: other things. total clean up?
        
        self.enabled = False
        
    def check(self):

        #TODO: check if compiz-fusion is running

        #TODO: check if dbus plugin is active

        #TODO: check if water plugin is active

        # check if we can get the root window id
        if (self.getWinID() == False):
            return (False, "Can't get root window id. Check xwininfo")

        return (True, 'Ok')

    def set_blinking(self, blinking):

        self._set_blinking(blinking)
        if (blinking == True): 

            # find coordinates x,y
            rect = self.controller.trayIcon.tray.get_geometry()[1]
            x = rect.x + rect.width/2
            y = rect.y + rect.height/2

            # run the water effect
            self.effectWater(x, y)
            
    def getWinID(self):

        '''this will retrieve the root window id'''

        #TODO: Creo que hay forma de hacerlo con GTK, sin tener que usar xwininfo
        try:
            rootwin = subprocess.Popen(['xwininfo', '-root'],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
        except OSError:
            return False
        try:
            rootwin_id = int(rootwin.split()[3], 0)
        except IndexError:
            return False
        except ValueError:
            return False

        return rootwin_id

    def effectWater(self, x, y):

        '''compiz water effect in coordinates x , y'''

        # interacts with compiz
        args = ['root', self.rootWindow, 'amplitude', 1, 'x', x, 'y', y]
        self.obj.activate(*args)

